#include <iostream>
#include <stdlib.h>
#include "../Puffer/Buffer.h"

using namespace std;

Buffer buffer("ebook.txt", "out.txt");

void test1(){
	for(int i = 0; i < 10;  i++){
		cout << buffer.getChar();
	}
	for(int i = 0; i < 5;  i++){
		buffer.ungetChar();
	}
	cout << endl;
	for(int i = 0; i < 10;  i++){
		cout << buffer.getChar();
	}
}

void testBufferSize(){
	for(int i = 0; i < 20000; i++){
		cout << buffer.getChar();
	}
}

void testUngetChar(){
	for(int i = 0; i < 20; i++){
		cout << buffer.getChar();
	}
	cout << endl;
	char* tmp = new char[100];
	buffer.copyString(12, tmp);
	for(int i = 1; i <= 12; i++){
		cout << tmp[i];
	}
}

void testWriteString(){
	buffer.writeString("Hello World!\n");
	buffer.writeString("Hello blub!\n");
	buffer.writeString("blabla World!\n");
	buffer.writeString("hihi World!\n");
	buffer.writeString("Hello tralu!\n");
	buffer.writeString("Heüäd!");
}

void testWriteString2(){
	buffer.writeString("1");
	buffer.writeString("2");
	buffer.writeString("3");
	buffer.writeString("4");
	buffer.writeString("5");
	buffer.writeString("6");
}

void testWriteString3(){
	char* tmp = new char[2000001];
	for(int j = 0; j < 2000000; j++){
		tmp[j] = buffer.getChar();
	}
	tmp[2000000] = '\0';
	buffer.writeString(tmp);

}

void testWriteString4(){
	char c;
	do{
		c = buffer.getChar();
		buffer.writeString(&c);
	}while(c != '\0');
}

void testCharsInBook(char test){
	int counter = 0;
	char c;
	do{
		c = buffer.getChar();
		if(c == test){
			counter++;
		}
	}while(c != '\0');
	char* tmp = new char[100];
	int x = 0;
	int counter2 = counter;
	while(counter2 > 0){
		counter2 /= 10;
		x++;
	}
	for(int i = x - 1; i >= 0; i--){
		tmp[i] = (counter % 10) + 48;
		counter /= 10;
	}
	tmp[x] = '\0';
	buffer.writeString(tmp);
}

void testCharsInBook2(char test){
	int counter = 0;
	char c;
	do{
		c = buffer.getChar();
		if(c == test){
			counter++;
		}
	}while(c != '\0');
	char* output = new char[100];
	sprintf(output, "the number of %C's in the file is: %i", test, counter);
	buffer.writeString(output);
}

int main(int argc, char* argv[]){

	//test1();

	//test2();

	//testUngetChar();

	//testWriteString2();

	//testWriteString();

	testCharsInBook2('e');

	return EXIT_SUCCESS;
}

